package icu.cqcai.xinsi.mapper;

import icu.cqcai.xinsi.domain.ChatSession;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 聊天会话Mapper接口
 */
@Mapper
public interface ChatSessionMapper {

    /**
     * 创建新的聊天会话
     */
    @Insert("INSERT INTO chat_sessions (user_id, title, status, last_message_time, create_time, update_time, message_count, coze_conversation_id, coze_user_id) " +
            "VALUES (#{userId}, #{title}, #{status}, #{lastMessageTime}, #{createTime}, #{updateTime}, #{messageCount}, #{cozeConversationId}, #{cozeUserId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertChatSession(ChatSession chatSession);

    /**
     * 根据用户ID获取聊天会话列表
     */
    @Select("SELECT * FROM chat_sessions WHERE user_id = #{userId} AND status = 1 ORDER BY last_message_time DESC")
    List<ChatSession> selectSessionsByUserId(@Param("userId") Long userId);

    /**
     * 根据会话ID获取会话信息
     */
    @Select("SELECT * FROM chat_sessions WHERE id = #{sessionId} AND status = 1")
    ChatSession selectSessionById(@Param("sessionId") Long sessionId);

    /**
     * 更新会话的最后消息时间和消息数量
     */
    @Update("UPDATE chat_sessions SET last_message_time = #{lastMessageTime}, message_count = message_count + 1, " +
            "update_time = #{updateTime} WHERE id = #{sessionId}")
    int updateSessionLastMessage(@Param("sessionId") Long sessionId, 
                                @Param("lastMessageTime") LocalDateTime lastMessageTime,
                                @Param("updateTime") LocalDateTime updateTime);

    /**
     * 更新会话标题
     */
    @Update("UPDATE chat_sessions SET title = #{title}, update_time = #{updateTime} WHERE id = #{sessionId}")
    int updateSessionTitle(@Param("sessionId") Long sessionId, 
                          @Param("title") String title,
                          @Param("updateTime") LocalDateTime updateTime);

    /**
     * 删除会话（软删除）
     */
    @Update("UPDATE chat_sessions SET status = 0, update_time = #{updateTime} WHERE id = #{sessionId}")
    int deleteSession(@Param("sessionId") Long sessionId, @Param("updateTime") LocalDateTime updateTime);

    /**
     * 获取用户的活跃会话数量
     */
    @Select("SELECT COUNT(*) FROM chat_sessions WHERE user_id = #{userId} AND status = 1")
    int countActiveSessionsByUserId(@Param("userId") Long userId);

    /**
     * 获取用户最近的会话
     */
    @Select("SELECT * FROM chat_sessions WHERE user_id = #{userId} AND status = 1 " +
            "ORDER BY last_message_time DESC LIMIT 1")
    ChatSession selectLatestSessionByUserId(@Param("userId") Long userId);

    /**
     * 更新会话状态
     */
    @Update("UPDATE chat_sessions SET status = #{status}, update_time = NOW() WHERE id = #{sessionId}")
    int updateSessionStatus(@Param("sessionId") Long sessionId, @Param("status") int status);

    /**
     * 更新会话的扣子会话ID
     */
    @Update("UPDATE chat_sessions SET coze_conversation_id = #{cozeConversationId}, update_time = #{updateTime} WHERE id = #{sessionId}")
    int updateCozeConversationId(@Param("sessionId") Long sessionId, 
                                @Param("cozeConversationId") String cozeConversationId,
                                @Param("updateTime") LocalDateTime updateTime);
} 